package org.example.springsecurity_.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class MyUserDetails implements UserDetails {
    private User user;
    private List<String> permissions;
    public void setUser(User user){
        this.user=user;
    }
    public User getUser() {
        return user;
    }
    public List<String> getPermissions() {
        return permissions;
    }
    public void setPermissions(List<String> permissions) {
        this.permissions = permissions;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 返回用户的权限列表，假设用户没有权限
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities=new ArrayList<>();
        for (int i=0;i< permissions.size();i++){
            simpleGrantedAuthorities.add(new SimpleGrantedAuthority(permissions.get(i)));
        }
        return simpleGrantedAuthorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }
}
